//
//  DYToastLaebl.m
//  ICBCSuisseCredit
//
//  Created by Victor on 2017/4/26.
//  Copyright © 2017年 Victor. All rights reserved.
//

#import "DYToastLaebl.h"


static DYToastLaebl * dyToastLabel = nil;

@implementation DYToastLaebl


+(DYToastLaebl *)share
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dyToastLabel = [[DYToastLaebl alloc] initWithFrame:CGRectMake((kSCREEN_HEIGHT-230*kAUTOLAYOUTSACLE)/2, (KScreenHeight-45*kAUTOLAYOUTSACLE)/2, 230*kAUTOLAYOUTSACLE,45*kAUTOLAYOUTSACLE )];
    });
    return dyToastLabel;
}

-(void)showMessage:(NSString *)message WithTime:(NSInteger)time
{
    DYToastLaebl *dyToastLabel = [[DYToastLaebl alloc] initWithFrame:CGRectMake(15, KScreenHeight-45*kAUTOLAYOUTSACLE-55,KScreenWidth-30,45*kAUTOLAYOUTSACLE )];
    dyToastLabel.textColor=[UIColor whiteColor];
    dyToastLabel.lineBreakMode=0;
    dyToastLabel.layer.masksToBounds=YES;
    dyToastLabel.layer.cornerRadius=8;
    dyToastLabel.backgroundColor=[UIColor blackColor];
    dyToastLabel.textAlignment=NSTextAlignmentCenter;
    dyToastLabel.text=message;
    dyToastLabel.font = kFontOfSize(13);
    CGSize size = [self setLabelWidth_box:message font:dyToastLabel.font with:KScreenWidth-60 with:45*kAUTOLAYOUTSACLE];
    size.width = size.width+30;
    size.height = size.height+14;
    CGRect frame = dyToastLabel.frame;
    frame.size = size;
    
    frame.origin.x = (KScreenWidth-size.width)/2;
    dyToastLabel.frame = frame;
    [UIView animateWithDuration:time animations:^{
        dyToastLabel.alpha=0.1;
        dyToastLabel.alpha=0.3;
        dyToastLabel.alpha=0;
    } completion:^(BOOL finished) {
        [dyToastLabel removeFromSuperview];
    }];
    [HDkeyWindow addSubview:dyToastLabel];


}
//设置label宽度
- (CGSize)setLabelWidth_box:(NSString *)str font:(UIFont*)font with:(CGFloat)width with:(CGFloat)height
{
    //    [UIFont fontWithName:@"Helvetica" size:15]
    CGSize size1 = CGSizeMake(width, height);
    //获取当前文本的属性
    NSDictionary *tdic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
    CGSize labelSize = [str boundingRectWithSize:size1 options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:tdic context:nil].size;
    return labelSize;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
